package com.btks.mapreduce.outputformat;

import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.RecordWriter;
import org.apache.hadoop.mapreduce.TaskAttemptContext;

import java.io.IOException;

/**
 * @author yiwannuofulasi
 * @description
 * @date 2023/3/27 11:27
 * Copyright (c) 2023/3/27 by yiwannuofulasi yiwannuofulasi@163.com, by 新疆兵团勘测设计院（集团）有限公司 All Rights Reserved.
 */
public class LogRecordWriter extends RecordWriter<Text, NullWritable> {

    FSDataOutputStream outputStream1;

    FSDataOutputStream outputStream2;

    public LogRecordWriter(TaskAttemptContext taskAttemptContext) {
        // 创建两个流
        try {
            FileSystem fileSystem = FileSystem.get(taskAttemptContext.getConfiguration());
            outputStream1 = fileSystem.create(new Path("D:\\devTool\\VMTool\\hadoop\\result\\baidu.log"));
            outputStream2 = fileSystem.create(new Path("D:\\devTool\\VMTool\\hadoop\\result\\other.log"));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void write(Text text, NullWritable nullWritable) throws IOException {
        String s = text.toString();
        if (s.contains("baidu")){
            outputStream1.writeBytes(s+"\n");
        }else {
            outputStream2.writeBytes(s+"\n");
        }
    }

    @Override
    public void close(TaskAttemptContext taskAttemptContext) throws IOException, InterruptedException {
        IOUtils.closeStream(outputStream1);
        IOUtils.closeStream(outputStream2);
    }
}
